package com.moshang.blog.service.impl;

import com.moshang.blog.core.utils.RedisKeyUtils;
import com.moshang.blog.entity.TblGreat;
import com.moshang.blog.entity.bo.LikeCount;
import com.moshang.blog.entity.bo.LikeStatus;
import com.moshang.blog.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xww
 * Created on 2019/4/25.
 */
@Service
@Slf4j
public class RedisServiceImpl  implements RedisService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void saveLiked2Redis(Integer likedUserId, Integer articleId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, articleId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikeStatus.LIKE.getCode());
    }

    @Override
    public void unlikeFromRedis(Integer likedUserId, Integer likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, likedPostId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikeStatus.UNLIKE.getCode());
    }
    @Override
    public Integer findLikedFromRedis(Integer likedUserId, Integer articleId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, articleId);
      Integer code= (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
      return code;
    }

    @Override
    public void incrementLikedCount(Integer articleId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, articleId, 1);
    }

    @Override
    public void decrementLikedCount(Integer articleId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, articleId, -1);
    }

    @Override
    public Integer findLikedCount(Integer articleId) {
        Integer code= (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, articleId);
        if(code==null){
            code=0;
        }
        return code;
    }

    @Override
    public List<TblGreat> getLikedDataFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED, ScanOptions.NONE);
        List<TblGreat> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object, Object> entry = cursor.next();
            String key = (String) entry.getKey();
            //分离出 likedUserId，articleId
            String[] split = key.split("::");
            Integer likedUserId = Integer.valueOf(split[0]);
            Integer articleId = Integer.valueOf(split[1]);
            Integer value = (Integer) entry.getValue();
                //组装成 TblGreat 对象
            TblGreat tblGreat = new TblGreat(articleId,likedUserId,value);
            list.add(tblGreat);
            }
//            //存到 list 后从 Redis 中删除
//            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
        return  list;
        }

    @Override
    public List<LikeCount> getLikedCountFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, ScanOptions.NONE);
        List<LikeCount> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object, Object> map = cursor.next();
            Integer key = (Integer)map.getKey();
            LikeCount tbArticleInfo = new LikeCount(key, (Integer) map.getValue());
            list.add(tbArticleInfo);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, key);
        }
        return list;
    }
}
